**************************************************************
*This file builds measures of robot exposure and other shocks*
**************************************************************

*********************
*Stacked differences*
*********************

use "$clean_data_automation/czones_exposure_robots.dta", clear

*Keep relevant dates*
rename *93_00_ *_y1990
rename *00_07_ *_y2000
rename *07_14_ *_y2007

keep czone-name_czone *_y1990 *_y20*

*Merge additional data (contributed from Lerch (2022))*
merge 1:1 czone using "$clean_data_automation/expo_trends.dta", assert(3) nogenerate keepusing(*_y1990 *_y2000)
merge 1:1 czone using "$contributed_data/expo_occs_1970.dta", assert(3) nogenerate
merge 1:1 czone using "$contributed_data/expo_occs_1990.dta", assert(3) nogenerate
merge 1:1 czone using "$contributed_data/expo_occs_2000.dta", assert(3) nogenerate
merge 1:1 czone using "$contributed_data/expo_occs_2008.dta", assert(3) nogenerate

rename expo_share_*1990 expo_share_*y1990
rename expo_share_*2000 expo_share_*y2000

*Create stacked differences*
keep czone *_y19* *_y20*
order *_y19* *_y20*

*obtain list of variables to reshape*
quietly: describe *y1990, varlist
local varlist = r(varlist)
local finalvar: subinstr local varlist "_y1990" "_y", all
reshape long `finalvar' , i(czone) j(year)
rename *_y *

*Merge exposure indices thata are time invariant*
merge m:1 czone using "$clean_data_automation/expo_trends.dta", assert(3) nogenerate
merge m:1 czone using "$clean_data_automation/expo_it_computers.dta", assert(1 3) nogenerate
merge m:1 czone using "$contributed_data/expo_occs_1970.dta", assert(3) nogenerate
merge m:1 czone using "$contributed_data/expo_occs_1990.dta", assert(3) nogenerate

drop *_y19* *_y20* *1990_20*

replace year = 2008 if year == 2007

*Save*
save "$final_data_automation/czones_ExposureAutomation.dta", replace

******************
*Long differences*
******************

use "$clean_data_automation/czones_exposure_robots.dta", clear
rename *_ *

*Merge exposure to other industry-level shocks*
merge 1:1 czone using "$clean_data_automation/expo_it_computers.dta", assert(1 3) nogenerate
merge 1:1 czone using "$clean_data_automation/expo_trends.dta", assert(3) nogenerate
merge 1:1 czone using "$contributed_data/expo_occs_1970.dta", assert(3) nogenerate
merge 1:1 czone using "$contributed_data/expo_occs_1990.dta", assert(3) nogenerate
merge 1:1 czone using "$contributed_data/expo_occs_2000.dta", assert(3) nogenerate

* chinese exposure post 2000's *
*gen chinashock_iv_00_07=chinashock_iv_y2000
drop *_y1990 *_y2000

*Save*
save "$final_data_automation/czones_ExposureAutomationLong.dta", replace

*Keep only year changes
use "$final_data_automation/czones_ExposureAutomationLong.dta", clear
keep czone expof_euro7_qo93_07 expof_euro5_qo93_07 expof_euro5_qo93_14 expof_euro7_qo00_14 expof_euro7_qo07_14 expof_euro7_qo93_14 expof_us_adj93_00 expof_us_adj00_07 expof_us_adj93_07 expof_us_adj93_14 expo_share_occ_routine_1990 expo_share_occ_offshore_1990 expo_comp_intensity_1993 expo_comp_intensity_1989 expo_it_intensity_1977 expo_it_intensity_1992
save "$final_data_automation/czones_ExposureAutomationLong.dta", replace
